﻿namespace ComicsDisplay

open System.IO
open System.Collections.Generic
open System.Xml.Serialization

type BookmarkEntry =
  val mutable Name:string
  val mutable Value:int
  new() = { Name = null; Value = 0 }

type Bookmarks =
  [<XmlIgnore>]
  val comics:Dictionary<string,int>
  new() = { comics=new Dictionary<string,int>() }

  [<XmlArray("BookmarksCollection")>]
  member x.Entries
    with get() =
      let a = new List<BookmarkEntry>()
      for e in x.comics do a.Add(new BookmarkEntry(Name=e.Key,Value=e.Value))
      a.ToArray()
    and set(v:BookmarkEntry array) =
      for e in v do x.comics.Add(e.Name, e.Value)

  member x.Save(fn:string) =
    let ser = new XmlSerializer(typeof<Bookmarks>)
    if File.Exists(fn) then File.Delete(fn)
    use f = File.Create(fn)
    ser.Serialize(f, x)
    
  static member Load(fn:string) =
    let ser = new XmlSerializer(typeof<Bookmarks>)
    use f = File.OpenRead(fn)
    ser.Deserialize(f) :?> Bookmarks


